<%--

  Event Registration component.

  

--%>
<%@include file="/libs/foundation/global.jsp"%>
<%@page session="false"%>
<%@page
	import="com.day.cq.wcm.foundation.forms.FormsManager,java.util.Calendar,java.util.Date,java.text.SimpleDateFormat"%>
<h1>registration form</h1>
<%
	int maxattendees = 0;
	int attendees = 0;
	boolean isOpen = true;
	String errMsg = "";
	Node eventNode = null;

	String eventPath = (String) properties.get("event");
	if (eventPath != null) {
		Resource eventResource = resource.getResourceResolver()
				.getResource(eventPath);

		if (eventResource != null) {
			eventNode = eventResource.adaptTo(Node.class);
			if (eventNode.hasProperty("maxAttendees")) {
				maxattendees = (int) eventNode.getProperty(
						"maxAttendees").getLong();
			}
		}
	}

	if (currentNode.hasNode("formpar")) {
		String actionPath = currentNode.getNode("formpar").getNode(
				"start").getProperty("action").getString();
		if (actionPath != null) {
			Resource actionResource = resource.getResourceResolver()
					.getResource(actionPath);
			if (actionResource != null) {
				Node actionNode = actionResource.adaptTo(Node.class);
				attendees = (int) actionNode.getNodes().getSize();
			}
		}
	}
	if (maxattendees < attendees) {
		isOpen = false;
		errMsg = "Maximum aantal registraties bereikt";
	}

	if (eventNode != null) {
		Calendar currentDate = Calendar.getInstance();
		Calendar startDate = null;
		Calendar endDate = null;
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");

		if (eventNode.hasProperty("subStart")) {
			String tmpDateStart = eventNode.getProperty("subStart")
					.getString();
			if (tmpDateStart != null) {
				Date dateStart = sdf.parse(tmpDateStart);
				startDate = Calendar.getInstance();
				startDate.setTime(dateStart);
			}
			if (startDate != null) {
				if (currentDate.compareTo(startDate) < 0) {
					isOpen = false;
					errMsg = "Inschrijving is nog niet geopend";
				}
			}
		}

		if (eventNode.hasProperty("subEnd")) {
			Date dateEnd = sdf.parse(eventNode.getProperty("subEnd")
					.getString());
			String tmpDateEnd = eventNode.getProperty("subEnd")
					.getString();
			if (tmpDateEnd != null) {
				endDate = Calendar.getInstance();
				endDate.setTime(dateEnd);
			}
			if (endDate != null) {
				if (currentDate.compareTo(endDate) > 0) {
					isOpen = false;
					errMsg = "Inschrijving is verlopen";
				}
			}
		}
	} else {
		isOpen = false;
		errMsg = "Er is geen event geconfigureerd";
		log.error("*** Er is geen event geconfigureerd");
	}

	if (isOpen) {
%><cq:include path="formpar" resourceType="foundation/components/parsys" />
<%
	} else {
%><%=errMsg%>
<%
	}
%>